import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:terminal_flutter_demo/font/CustomIcon.dart';
import 'package:terminal_flutter_demo/pages/animated_widget_page.dart';
import 'package:terminal_flutter_demo/pages/animation_list_page.dart';
import 'package:terminal_flutter_demo/pages/base_widget_page.dart';
import 'package:terminal_flutter_demo/pages/button_type_page.dart';
import 'package:terminal_flutter_demo/pages/card_circle_aspect_page.dart';
import 'package:terminal_flutter_demo/pages/column_row_page.dart';
import 'package:terminal_flutter_demo/pages/dialog_page.dart';
import 'package:terminal_flutter_demo/pages/getX_page.dart';
import 'package:terminal_flutter_demo/pages/grid_view_page.dart';
import 'package:terminal_flutter_demo/pages/list_view_page.dart';
import 'package:terminal_flutter_demo/pages/navigator_page.dart';
import 'package:terminal_flutter_demo/pages/page_view_page.dart';
import 'package:terminal_flutter_demo/pages/stack_align_page.dart';
import 'package:terminal_flutter_demo/pages/tab_bar_view_page.dart';
import 'package:terminal_flutter_demo/pages/widget_key_page.dart';
import 'package:terminal_flutter_demo/pages/wrap_page.dart';
import 'package:terminal_flutter_demo/widgets/my_drawer.dart';

void main() {
  runApp(
    GetMaterialApp(
      debugShowCheckedModeBanner: false, // 右上角debug图标是否显示
      theme: ThemeData(
        primaryColor: Colors.blue,
      ),
      home: const MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  final switchTitle = '切换';

  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isNavSwitch = true;

  bool _isDrawerSwitch = true;

  int _showIndex = 0;

  int _currentIndex = 0;

  late final List<Widget> widgets;

  Widget? _currendDrawerWidget;

  @override
  void initState() {
    super.initState();
    widgets = [
      const GetXPage(),
      const AnimatedWidgetPage(),
      const AnimationListPage(),
      const WidgetKeyPage(),
      const SwiperPage(),
      const PageViewPage(),
      const DialogPage(),
      const BaseWidget(),
      ListViewPage(),
      ListViewResPage(),
      GridViewPage(),
      ColumnRowPage(),
      const StackAlignPage(),
      CardCircleAspect(),
      const ButtonTypePage(),
      const WrapPage(),
      TabBarViewPage(),
      NavigatorPage(
        title: '首页',
      ),
    ];
  }

  // List<BottomNavigationBarItem> _navigationBarItems = [];

  Widget? _getShowPage() {
    if (_isDrawerSwitch && _currendDrawerWidget != null) {
      return _currendDrawerWidget;
    }
    if (_isNavSwitch) {
      return widgets[_currentIndex * 2];
    }
    return widgets[_showIndex];
  }

  get _navigationBarItems {
    return [
      const BottomNavigationBarItem(
        icon: Icon(
          Icons.home,
        ),
        label: '主页',
      ),
      const BottomNavigationBarItem(
        icon: Icon(
          CustomIcon.weixin,
        ),
        label: '微信',
      ),
      const BottomNavigationBarItem(
        icon: Icon(
          Icons.search,
        ),
        label: '搜索',
      ),
      const BottomNavigationBarItem(
        icon: Icon(
          CustomIcon.gouwuchekong,
        ),
        label: '购物车',
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Flutter')),
        body: _getShowPage(),
        drawer: MyDrawer(
          widgets: widgets,
          onTap: (Widget widget) {
            setState(() {
              _isDrawerSwitch = true;
              _currendDrawerWidget = widget;
            });
          },
        ),
        endDrawer: const Drawer(
          child: Text('右侧'),
        ),
        bottomNavigationBar: BottomNavigationBar(
          fixedColor: Colors.greenAccent, // 选中的颜色
          iconSize: 36,
          currentIndex: _currentIndex,
          type: BottomNavigationBarType.fixed, // 4个或4个以上须设置此属性
          onTap: (index) {
            setState(() {
              _isDrawerSwitch = false;
              _isNavSwitch = true;
              _currentIndex = index;
            });
          },
          items: _navigationBarItems,
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: Container(
          width: 60,
          height: 60,
          decoration: BoxDecoration(
            color: Colors.amber,
            borderRadius: BorderRadius.circular(30),
            boxShadow: const [
              BoxShadow(color: Colors.red, blurRadius: 10),
            ],
          ),
          child: FloatingActionButton(
            onPressed: () {
              setState(() {
                _isDrawerSwitch = false;
                _isNavSwitch = false;
                _showIndex++;
                if (_showIndex > widgets.length - 1) {
                  _showIndex = 0;
                }
              });
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(widget.switchTitle),
                // Text('$showIndex'),
                Text(widgets[_showIndex].toString()),
              ],
            ),
          ),
        ));
  }
}
